//
//  HotVC.m
//  Faker
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 dllo. All rights reserved.
//

#define Address @"http://c.3g.163.com/recommend/getSubDocNews?passport=&devId=862095021675571&size=20&version=5.3.3&from=yuedu&net=wifi"//请求地址

#import "ZGLHotVC.h"
#import <MJRefresh.h>
#import "ZGLHTTPTOOL.h"

#import "ZGLHotCell_a.h"
#import "ZGLHotCell_b.h"
#import "ZGLHotCell_c.h"
#import "ZGLHotCell_d.h"
#import "ZGLHotModel.h"

#import "ZGLHotDetailsVC.h"
#import "ZGL24hVC.h"
#import "ZGLmyVC.h"
#import "ZGLweather.h"
@interface ZGLHotVC ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *hotView;
@property(nonatomic, retain)NSMutableArray *modArr;
@property(nonatomic, assign)NSInteger cellheight;
@property(nonatomic, retain)ZGLweather *weather;
@end

@implementation ZGLHotVC
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.cellheight = 44;
        self.modArr = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = backColor;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"热点" style:(UIBarButtonItemStyleDone) target:self action:nil];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"lianxiren"] style:(UIBarButtonItemStyleDone) target:self action:@selector(barButtonItem:)];
    item.tag = 3000;
    item.tintColor = [UIColor whiteColor];
    UIBarButtonItem *item_t = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"24"] style:(UIBarButtonItemStyleDone) target:self action:@selector(barButtonItem:)];
    item_t.tintColor = [UIColor whiteColor];
    item_t.tag = 3001;
    self.navigationItem.rightBarButtonItems = @[item, item_t];
    
    [self createTableView];
    
    //开始请求数据
    [_hotView.header beginRefreshing];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = naviColor;
    _hotView.backgroundColor = backColor;
    self.view.backgroundColor = backColor;
    [_hotView reloadData];
}

- (void)barButtonItem:(UIBarButtonItem *)button {
    if (button.tag == 3000) {
        ZGLmyVC *my = [[ZGLmyVC alloc]init];
        [self.navigationController pushViewController:my animated:YES];
    }else if (button.tag == 3001) {
        ZGL24hVC *h24 = [[ZGL24hVC alloc]init];
        [self.navigationController pushViewController:h24 animated:YES];
    }
}

//创建tableview
- (void)createTableView{
    _hotView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _hotView.backgroundColor = backColor;
    _hotView.delegate = self;
    _hotView.dataSource = self;
    [_hotView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_hotView];
    
    //    注册cell
    [_hotView registerClass:[ZGLHotCell_a class] forCellReuseIdentifier:@"ZGLHotCell_a"];
    [_hotView registerClass:[ZGLHotCell_b class] forCellReuseIdentifier:@"ZGLHotCell_b"];
    [_hotView registerClass:[ZGLHotCell_c class] forCellReuseIdentifier:@"ZGLHotCell_c"];
    [_hotView registerClass:[ZGLHotCell_d class] forCellReuseIdentifier:@"ZGLHotCell_d"];
    
    //天气
    _weather = [[ZGLweather alloc]init];
    _weather.view.frame = CGRectMake(0, 0, Width, (int)250*ScaleX);
    _hotView.tableHeaderView = _weather.view;
    
    //    刷新和加载
    _hotView.tableFooterView = [[UIView alloc]init];
    __weak typeof(self) weakSelf = self;
    _hotView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_weather getAdd];
        [weakSelf handelDataWithURL:Address WithType:0];
    }];
    
    _hotView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf handelDataWithURL:Address WithType:1];
    }];
}

// MJRefrash请求数据
- (void)handelDataWithURL:(NSString *)url WithType:(NSInteger)type{
    __weak typeof(self) weakSelf = self;
    [ZGLHTTPTOOL GETWithURL:url withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        if (type == 0) {
            self.modArr = [[NSMutableArray alloc]initWithArray:[tempDic objectForKey:@"推荐"]];
        }else if (type == 1){
            [self.modArr addObjectsFromArray:[tempDic objectForKey:@"推荐"]];
        }
        [weakSelf.hotView reloadData];
        [weakSelf.hotView.header endRefreshing];
        [weakSelf.hotView.footer endRefreshing];
    }withFail:^(NSError *error) {
        NSLog(@"error:没网 ZGLHotVC");
        [weakSelf.hotView.header endRefreshing];
        [weakSelf.hotView.footer endRefreshing];
        
    }];
}

//判断cell种类
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGLHotModel *mod = [ZGLHotModel modWithDic:self.modArr[indexPath.row]];
    if ([mod.template_a isEqualToString:@"pic1"]) {
        ZGLHotCell_a *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLHotCell_a"];
        cell.mod = mod;
        self.cellheight = cell.height;
        return cell;
    }else if([mod.template_a isEqualToString:@"normal"]){
        ZGLHotCell_a *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLHotCell_b"];
        cell.mod = mod;
        self.cellheight = cell.height;
        return cell;
    }else if([mod.template_a isEqualToString:@"pic31"]) {
        ZGLHotCell_a *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLHotCell_c"];
        cell.mod = mod;
        self.cellheight = cell.height;
        return cell;
    }else{
        ZGLHotCell_a *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLHotCell_d"];
        cell.mod = mod;
        self.cellheight = cell.height;
        return cell;
    }
}

//动态设置cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellheight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modArr.count;
}

//cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.view.userInteractionEnabled = NO;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZGLHotDetailsVC *hotDetails = [[ZGLHotDetailsVC alloc]init];
    hotDetails.docid = [self.modArr[indexPath.row]objectForKey:@"docid"];
    [self.navigationController pushViewController:hotDetails animated:YES];
    self.view.userInteractionEnabled = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
